// pages/news/index.js
const app = getApp();
var sendPost = app.globalData.sendPost;
let globalData = getApp().globalData;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        news_list: [],
        qian_img: globalData.qian_img,
        currentPage: 1,
        totalDataCount: 10
    },
    //点击进入详情页
    newsInfoTap: function(e) {
        var id = e.currentTarget.dataset.id;
        let url = e.currentTarget.dataset.webView;
        let title = e.currentTarget.dataset.title;
        if (url) {
            wx.navigateTo({
                url: '../news_info_webview/index?url=' + url+'&title='+title,
            })
        } else {
            wx.navigateTo({
                url: '../news_info/index?news_id=' + id + '&title=' + title,
            })
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var _this = this;
        this.getNewsList();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        this.loadMoreData();
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    },
    getNewsList() {
        let data = {
            pid: globalData.pid,
            page: '1'
        };
        globalData.sendPost('article/list', data).then(res => {
            if (res.status == 1) {
                this.setData({
                    news_list: res.data
                })
            }
        })
    },
    loadMoreData() {
        var that = this;
        var currentPage = that.data.currentPage;
        currentPage += 1; //加在当前页面下一页数据
        var tips = '加载第' + (currentPage + 1) + '页';
        wx.showLoading({
            title: tips,
        });
        globalData.sendPost('article/list', data).then(res => {
            if (res.status == 1) {
                wx.hideLoading();
                var data = res.data;
                var articles = data.data;
                // 计算当前共加载了多少条数据，来证明这种方式可以加载更多数据
                var totalDataCount = that.data.totalDataCount;
                totalDataCount = totalDataCount + articles.length;
                // 直接将新一页的数据添加到数组里
                that.setData({
                    ["news_list[" + currentPage + "]"]: articles,
                    currentPage: currentPage,
                    totalDataCount: totalDataCount
                })
            }
        })
    }
})